using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DatabaseManager.Util;
using System.IO;
using DatabaseManager.Util.Exporters;

namespace DatabaseManager.UI
{
    /// <summary>
    /// The form to export the data, wich is displayed in the datagrid into several file formats
    /// </summary>
    public partial class FExportToFile : Form
    {
        /// <summary>
        /// Describes the type of the export format
        /// </summary>
        private enum ExportType
        {
            /// <summary>
            /// XML format
            /// </summary>
            XML,
            /// <summary>
            /// CSV format (comma or semicolon separated)
            /// </summary>
            CSV,
            /// <summary>
            /// Excel 97-2003 file
            /// </summary>
            Excel
        }

        private ExportType _selectedExportType = ExportType.XML;
        private DataTable _dataTableToExport;

        /// <summary>
        /// Initializes a new instance of the <see cref="FExportToFile"/> class.
        /// </summary>
        public FExportToFile()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the data table to export.
        /// </summary>
        /// <value>The data table to export.</value>
        public DataTable DataTableToExport
        {
            get { return _dataTableToExport; }
            set { _dataTableToExport = value; }
        }

        private void btnExportData_Click(object sender, EventArgs e)
        {
            DataExportBase dataExport;

            switch (_selectedExportType)
            {
                case ExportType.XML:
                    dataExport = new DataExportXml(
                        _dataTableToExport,
                        txtFileName.Text);
                    break;

                case ExportType.CSV:
                    dataExport = new DataExportCsv(
                        _dataTableToExport,
                        txtFileName.Text);
                    break;

                case ExportType.Excel:
                    throw new NotImplementedException("Exporting excel files is not implemented yet!");

                default:
                    throw new NotImplementedException("Export format '" + _selectedExportType.ToString() + "' has not been implemented yet!");
            }

            dataExport.DataExportProcess += new DataExportProcessHandler(dataExport_DataExportProcess);

            try
            {
                dataExport.Export();

                MessageBox.Show(
                    "Der Exportvorgang wurde abgeschlossen!",
                    Text,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information,
                    MessageBoxDefaultButton.Button1);
            }
            catch (Exception ex)
            {
                Utility.ShowExceptionBox(ex, "Fehler beim Exportieren");
            }
        }

        private void dataExport_DataExportProcess(object sender, DataExportProcessEventArgs e)
        {
            pbExportState.Value = (int)((double)e.CurrentRowIndex / (double)e.RowCount * 100);
        }

        private void btnSelectFile_Click(object sender, EventArgs e)
        {
            sfdExportFile.FileName = txtFileName.Text;

            DialogResult result = sfdExportFile.ShowDialog();

            if (result == DialogResult.OK)
            {
                txtFileName.Text = sfdExportFile.FileName;
                btnExportData.Enabled = true;
            }
        }

        private void SetExportType(ExportType type)
        {
            string exportFileFilter = string.Empty;
            string defaultExtension = string.Empty;
            string pathWithoutExtension = string.Empty;

            if (txtFileName.Text != string.Empty)
            {
                pathWithoutExtension = Path.Combine(
                                        Path.GetDirectoryName(txtFileName.Text),
                                        Path.GetFileNameWithoutExtension(txtFileName.Text));
            }

            _selectedExportType = type;

            switch (type)
            {
                case ExportType.XML:
                    defaultExtension = "xml";
                    exportFileFilter = "XML-Dateien (*.xml)|*.xml";
                    break;
                case ExportType.CSV:
                    defaultExtension = "csv";
                    exportFileFilter = "CSV-Dateien (*.csv)|*.csv";
                    break;
                case ExportType.Excel:
                    defaultExtension = "xls";
                    exportFileFilter = "Excel 97-2003-Dateien (*.xls)|*.xls";
                    break;
                default:
                    throw new NotImplementedException("Export format '" + _selectedExportType.ToString() + "' has not been implemented yet!");
            }

            sfdExportFile.Filter = exportFileFilter;
            sfdExportFile.DefaultExt = defaultExtension;

            if (txtFileName.Text != string.Empty)
            {
                txtFileName.Text = pathWithoutExtension + "." + defaultExtension;
            }
        }

        private void rdExportToXML_CheckedChanged(object sender, EventArgs e)
        {
            if (rdExportToXML.Checked)
            {
                SetExportType(ExportType.XML);
            }
        }

        private void rdExportToCSV_CheckedChanged(object sender, EventArgs e)
        {
            if (rdExportToCSV.Checked)
            {
                SetExportType(ExportType.CSV);
            }
        }

        private void FExportToFile_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            Help.ShowHelp(
                this,
                Utility.GetHelpFileName(),
                HelpNavigator.TopicId,
                HelpTopics.FunctionsExportToFile);

            e.Cancel = true;
        }
    }
}